using System;
using System.Collections.Generic;
using WegasManagementSystem.DAL;
using WegasManagementSystem.Models;

namespace WegasManagementSystem.BLL
{
    /// <summary>
    /// 员工业务逻辑类
    /// </summary>
    public class EmployeeBLL
    {
        private EmployeeDAL employeeDAL;
        private DepartmentDAL departmentDAL;

        /// <summary>
        /// 构造函数
        /// </summary>
        public EmployeeBLL()
        {
            employeeDAL = new EmployeeDAL();
            departmentDAL = new DepartmentDAL();
        }

        /// <summary>
        /// 添加员工
        /// </summary>
        /// <param name="employee">员工对象</param>
        /// <returns>新员工ID，失败返回0</returns>
        public int AddEmployee(Employee employee)
        {
            if (employee == null)
                throw new ArgumentNullException("employee", "员工对象不能为空");

            if (string.IsNullOrEmpty(employee.EmpNumber))
                throw new ArgumentException("工号不能为空");

            if (string.IsNullOrEmpty(employee.Name))
                throw new ArgumentException("姓名不能为空");

            if (employee.DeptId <= 0)
                throw new ArgumentException("必须选择部门");

            // 检查工号是否已存在
            if (employeeDAL.CheckEmpNumberExists(employee.EmpNumber))
                throw new Exception("工号已存在");

            // 检查部门是否存在
            Department department = departmentDAL.GetDepartmentById(employee.DeptId);
            if (department == null)
                throw new Exception("所选部门不存在");

            return employeeDAL.AddEmployee(employee);
        }

        /// <summary>
        /// 更新员工信息
        /// </summary>
        /// <param name="employee">员工对象</param>
        /// <returns>更新成功返回true，失败返回false</returns>
        public bool UpdateEmployee(Employee employee)
        {
            if (employee == null)
                throw new ArgumentNullException("employee", "员工对象不能为空");

            if (string.IsNullOrEmpty(employee.EmpNumber))
                throw new ArgumentException("工号不能为空");

            if (string.IsNullOrEmpty(employee.Name))
                throw new ArgumentException("姓名不能为空");

            if (employee.DeptId <= 0)
                throw new ArgumentException("必须选择部门");

            // 检查工号是否已存在（排除当前员工）
            if (employeeDAL.CheckEmpNumberExists(employee.EmpNumber, employee.EmpId))
                throw new Exception("工号已存在");

            // 检查部门是否存在
            Department department = departmentDAL.GetDepartmentById(employee.DeptId);
            if (department == null)
                throw new Exception("所选部门不存在");

            return employeeDAL.UpdateEmployee(employee);
        }

        /// <summary>
        /// 删除员工
        /// </summary>
        /// <param name="empId">员工ID</param>
        /// <returns>删除成功返回true，失败返回false</returns>
        public bool DeleteEmployee(int empId)
        {
            if (empId <= 0)
                throw new ArgumentException("无效的员工ID");

            return employeeDAL.DeleteEmployee(empId);
        }

        /// <summary>
        /// 获取所有员工
        /// </summary>
        /// <returns>员工列表</returns>
        public List<Employee> GetAllEmployees()
        {
            return employeeDAL.GetAllEmployees();
        }

        /// <summary>
        /// 根据部门获取员工
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <returns>员工列表</returns>
        public List<Employee> GetEmployeesByDepartment(int deptId)
        {
            if (deptId <= 0)
                throw new ArgumentException("无效的部门ID");

            return employeeDAL.GetEmployeesByDepartment(deptId);
        }

        /// <summary>
        /// 根据ID获取员工
        /// </summary>
        /// <param name="empId">员工ID</param>
        /// <returns>员工对象</returns>
        public Employee GetEmployeeById(int empId)
        {
            if (empId <= 0)
                throw new ArgumentException("无效的员工ID");

            return employeeDAL.GetEmployeeById(empId);
        }
    }
} 